package com.example.spring.exercise.service.impl;

import com.example.spring.exercise.mapper.CheckMapper;
import com.example.spring.exercise.service.CheckService;
import com.example.spring.exercise.service.dto.check.ClassCheckAbsentDTO;
import com.example.spring.exercise.service.dto.check.ClassInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author : zhayh
 * @date : 2021-5-22 20:03
 * @description :
 */

@Slf4j
@Service
public class CheckServiceImpl implements CheckService {
    @Resource
    private CheckMapper checkMapper;

    @Override
    @Cacheable(cacheNames = "system-absent", key = "#className+'-'+#termNo")
    public ClassCheckAbsentDTO getClassCheckAbsent(String className, String termNo) {
        log.info("获取班级缺勤率方法启动，输入参数：{}, {}", className, termNo);
        long time0 = System.currentTimeMillis();
        //获取班级信息
        ClassInfo classInfo = checkMapper.getClassInfoByClassName(className);
        //模拟数据延迟
        try {
            Thread.sleep(1500L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long time1 = System.currentTimeMillis();
        log.info("查询班级信息耗时：{} ms", (time1 - time0));
        //2.查询班级总缺勤次数
        Integer classAbsentCount = checkMapper.getClassAbsentCountByClassNoAndTerm(classInfo.getClassNo(), termNo);
        //模拟数据延迟
        try {
            Thread.sleep(1500L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long time2 = System.currentTimeMillis();
        log.info("查询班级缺勤率耗时：{} ms", (time2 - time1));
        //3.查询部门总缺勤次数
        Integer depAbsentCount =
                this.checkMapper.getDepAbsentCountByClassNoAndTerm(classInfo.getDeptInfo().getDepNo(), termNo);
        //模拟数据延迟
        try {
            Thread.sleep(1500L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //算出班级旷课总数占部门旷课总数比例
        Float absentRatio = (float) classAbsentCount / depAbsentCount;
        long time3 = System.currentTimeMillis();
        log.info("查询部门缺勤率耗时：{} ms", (time3 - time2));
        //4.组装结果类型
        ClassCheckAbsentDTO result = new ClassCheckAbsentDTO(classInfo, termNo, classAbsentCount, depAbsentCount, absentRatio);
        long time4 = System.currentTimeMillis();
        log.info("获取班级缺勤率方法完成，总用时：{}ms", (time4 - time0));
        return result;
    }

}
